package net.amsoft.iservice.management.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.struts.form.LinksUploadForm;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Gets called when an ibroker admin opts to change the static links for an 
 * iservice, enters links and their destination URLs and clicks "Submit" 
 * button <br>
 * 
 * @author mukta
 */
public class LinksUploadAction extends Action 
{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		// Get Form Data
		LinksUploadForm myForm = (LinksUploadForm) form;

		if ((isCancelled(request)) || ("cancel".equals(myForm.getAction())))
		{
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Cancelled Links Upload..");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to=" + IBrokerConstants.FWD_CANCEL);
			return mapping.findForward(IBrokerConstants.FWD_CANCEL);
		}
		
		// Check if user has come through proper channel or simply typed URL in browser
		String name[] = myForm.getName();
		String address[] = myForm.getAddress();
	    if ((name==null) && (address==null))
	    {
            IServiceUtil.log(IServiceConstants.DEBUG, this, "execute(): redirect to=" + 
            		IBrokerConstants.FWD_LOGIN);
	        return mapping.findForward(IBrokerConstants.FWD_LOGIN);
	    }       

		String linksData = "";
		int len = name.length;
		
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): Links Names -> " + name);
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): Links Addresses -> " + address);

	    for (int i=0; i<len; i++)
		{
	        name[i] = name[i].trim();
	        address[i] = address[i].trim();
			if ((!("".equals(name[i]))) && (!("".equals(address[i]))))
			{
			    if (address[i].indexOf("://") < 0)
			        address[i] = "http://" + address[i];
				if (!("".equals(linksData)))
					linksData = linksData + ", " + name[i] + "=" + address[i];
				else
					linksData = name[i] + "=" + address[i];
			}
		}

        IServiceUtil.log(IServiceConstants.INFO, this, 
                "execute(): Setting session attribute LinksData=" + linksData);
		request.getSession().setAttribute("LinksData", linksData);
		
        IServiceUtil.log(IServiceConstants.DEBUG, this, 
                "execute(): redirect to="+ IBrokerConstants.FWD_SUCCESS);
		return mapping.findForward(IBrokerConstants.FWD_SUCCESS);
	}
}